package com.auto.service.impl.coder.tree.web;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.auto.annotation.Column;
import com.auto.annotation.Table;
import com.auto.service.impl.BaseCoder;
import com.auto.service.impl.Coder;
import com.common.ArrayUtils;
import com.common.CommonUtil;
import com.common.Global;
/**
 * 处理页面所有的模板
 * @author chenfuqiang
 *
 */
public class JS_ModelCoder extends BaseCoder implements Coder{
	
	private final String TEMPLATE_PATH = "/tree/web/model.ftl";
	private final String CLASSTAIL = ".js";
	
	@Override
	public Map<String, Object> getRoot(Class<?> pojo) {
		Map<String,Object> root = new HashMap<String, Object>();
		root.put("package", Global.JS_MODEL_PACKAGE);
		root.put("beanPackage", Global.PACKAGEOFPOJO);
		root.put("daoPackage", Global.DAO_PACKAGE);
		root.put("servicePackage", Global.SERVICE_PACKAGE);
		root.put("author", Global.AUTHOR);
		root.put("className", pojo.getSimpleName());
		root.put("paramName", pojo.getSimpleName().substring(0,1).toLowerCase()+pojo.getSimpleName().substring(1));
		String tableName = pojo.getAnnotation(Table.class).name();
		if(CommonUtil.isEmpty(tableName)) {
			tableName = pojo.getSimpleName();
		}
		tableName = tableName.replace("@YEAR", "");
		root.put("tableName", tableName);
		Field[] fields = pojo.getDeclaredFields();
		//是否需要父类的字段。
//		AutoCode ac = pojo.getAnnotation(AutoCode.class);
//		if(ac.includeSupperClass()) {
			if(pojo.getSuperclass()!=null){
				Class<?> clsSup = pojo.getSuperclass();
				fields = (Field[]) ArrayUtils.addAll(fields,clsSup.getDeclaredFields());
			}
//		}
		
		StringBuffer fieldStr = new StringBuffer();//store like  id,username,age,...
		List<Map<String,String>> beans = new ArrayList<Map<String,String>>();
		for(Field field : fields) {
			Column column = field.getAnnotation(Column.class);
			if(column == null) {
				continue;
			}
			String dbcname = CommonUtil.isEmpty(column.name())?field.getName():column.name();
			String proper = field.getName();
			String type = CommonUtil.isEmpty(column.type())?"VARCHAR":column.type().toUpperCase();
			String jstype = CommonUtil.isEmpty(column.jstype())?column.type().toUpperCase():column.jstype();
			if(jstype.indexOf("VARCHAR")!=-1) {
				type="string";
			}else if(jstype.indexOf("INT")!=-1) {
				type="int";
			}else if(jstype.indexOf("DECIMAL")!=-1) {
				type="float";
			}else {
				type = jstype;
			}
			if(fieldStr.length()==0) {
				fieldStr.append(dbcname);
			}else {
				fieldStr.append(","+dbcname);
			}
			Map<String,String> map = new HashMap<String, String>();
			map.put("name", proper);
			map.put("type", type);
			beans.add(map);
		}
		root.put("beans", beans);
		root.put("columnFields",fieldStr.toString());
		
		return root;
	}
	
	@Override
	public String getTargetPackage() {
		return Global.JS_MODEL_PACKAGE;
	}

	@Override
	public String getSrcPath() {
		return Global.JSSRCPATH;
	}

	@Override
	public String getFtlPath() {
		return this.TEMPLATE_PATH;
	}

	@Override
	public String getClassTail() {
		return this.CLASSTAIL;
	}

}
